package routers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/plugins/cors"
	"h1/controllers"
)

func InitRouter() {
	beego.Router("/", &controllers.MainController{})
	//beego.Router("/admin", &admin.UserController{})
	//beego.Router("/admin/index", &admin.ArticleController{})
	//beego.Router("/admin/addpkg", &admin.AddController{})

	//跨域设置
	beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
		AllowAllOrigins:  true,
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
		ExposeHeaders:    []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
		AllowCredentials: true,
	}))
	FilterHandle() // 路由过滤操作
	unAuthAPI()    // 不需要验证
	authAPI()      // 需要验证
}

// 需要验证
func authAPI() {
	/*
		//用户相关信息
		// 用户信息相关增删改
		ns := beego.NewNamespace("/v1/auth",
			// 日志记录
			beego.NSNamespace("/log",
				beego.NSRouter("/project-log", &controllers.LoggersController{}, "post:GetProjectEditLogger"),
			),
		)
		beego.AddNamespace(ns)*/
}

// 不需要验证
func unAuthAPI() {
	auth :=
		beego.NewNamespace("/v1/api",
			beego.NSNamespace("/user",
				beego.NSRouter("/register", &controllers.UserLoginController{}, "post:Register"),
				beego.NSRouter("/login", &controllers.UserLoginController{}, "post:Login"),
			),
			beego.NSNamespace("/test",
				//localhost:8080/v1/api/get
				//localhost:8080/v1/api/post
				beego.NSRouter("/get", &controllers.TestController{}, "get:One"),
				beego.NSRouter("/two", &controllers.TestController{}, "post:Two"),
				beego.NSRouter("/three", &controllers.TestController{}, "post:Three"),
				beego.NSRouter("/four", &controllers.TestController{}, "post:Four"),
				beego.NSRouter("/five", &controllers.TestController{}, "post:Five"),
				beego.NSRouter("/six", &controllers.TestController{}, "post:Six"),
				beego.NSRouter("/seven", &controllers.TestController{}, "post:Seven"),
				beego.NSRouter("/eight", &controllers.TestController{}, "post:Eight"),
				beego.NSRouter("/selectOne", &controllers.TestController{}, "post:SelectOne"),
				beego.NSRouter("/jsonSerial", &controllers.TestController{}, "post:JsonSerial"),
				beego.NSRouter("/mapSerial", &controllers.TestController{}, "post:MapSerial"),
				beego.NSRouter("/sliceSerial", &controllers.TestController{}, "post:SliceSerial"),
				beego.NSRouter("/mapTest", &controllers.TestController{}, "post:MapTest"),
				beego.NSRouter("/jsonAndMap", &controllers.TestController{}, "post:JsonAndMap"),
				beego.NSRouter("/pointTest", &controllers.TestController{}, "post:PointTest"),
				beego.NSRouter("/pointTestX", &controllers.TestController{}, "post:PointTestX"),
				beego.NSRouter("/sliceSort", &controllers.TestController{}, "post:SliceSort"),
				beego.NSRouter("/operateFile", &controllers.TestController{}, "post:OperateFile"),
				beego.NSRouter("/inter", &controllers.TestController{}, "post:Inter"),
				beego.NSRouter("/cpu", &controllers.TestController{}, "post:Cpu"),
				beego.NSRouter("/chan", &controllers.TestController{}, "post:Chan"),
				beego.NSRouter("/reflect", &controllers.TestController{}, "post:Reflect"),
				beego.NSRouter("/redis", &controllers.TestController{}, "post:Redis"),
			),
		)
	beego.AddNamespace(auth)
}
